package com.bltec.emergency.controller.foreignController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ForeignBase;
import com.bltec.emergency.service.foreignService.ForeignBaseService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/foreignBase")
@RequiresRoles("superManger")
public class ForeignBaseController {
    @Resource
    private ForeignBaseService foreignBaseService;
    @Resource
    private UserService userService;


    @RequestMapping("getAllForeignBase")
    public ResponseJson getUser() {
        try {
            List<ForeignBase> recordForeignBase = foreignBaseService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(recordForeignBase));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }


    /*    private Long id;
        private String searchArea;
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date occurredTime;
        private String details;*/
    @RequestMapping("addForeignBase")
    public ResponseJson addUser(String searchArea, Date occurredTime, String details) {
        try {
            ForeignBase foreignBase = new ForeignBase();
            foreignBase.setSearchArea(searchArea);
            foreignBase.setOccurredTime(occurredTime);
            foreignBase.setDetails(details);
            foreignBase.setEnable(true);
            foreignBase.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignBase.setCreateTime(DateUtil.dateNow());
            foreignBaseService.add(foreignBase);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }


    @RequestMapping("updateForeignBase")
    public ResponseJson updateUser(Long id, String searchArea, Date occurredTime, String details) {
        try {
            ForeignBase foreignBase = foreignBaseService.getForeignBaseById(id);
            foreignBase.setSearchArea(searchArea);
            foreignBase.setOccurredTime(occurredTime);
            foreignBase.setDetails(details);
            foreignBase.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            foreignBase.setUpdateTime(DateUtil.dateNow());
            foreignBaseService.updateForeignBaseById(foreignBase);
            return new ResponseJson(Constant.OK, JSON.toJSON(foreignBase));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }


    @RequestMapping("deleteForeignBase")
    public ResponseJson setDisable(Long id) {
        if (null != foreignBaseService.getForeignBaseById(id)) {
            if (foreignBaseService.deleteForeignBaseById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
